/**
 * ModuleName: wx_mp_autoreply`
 * Company: MT
 * @author Kenny
 * @date 2018/1/03
 * @version 1.0
 * Description: 微信公众号自动回复
 * 
 * ......................我佛慈悲......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *                       
 *..................佛祖开光 ,永无BUG...................
 * 
 */
'use strict';

var enums = require('../_data/enums')
var Wxmpaccount = require('./wx-mp-account.js')

module.exports = function(Wxmpautoreply) {
    Wxmpautoreply.validatesLengthOf('key',{
        min: 4,
        max: 12,
        message:{
            min: 'is too short!',
            max: 'is too long!'
        }
    })
    Wxmpautoreply.beforeRemote('create', function(context, user, next) {
        console.log(context)
        next();
    })

    var _checkAccountExists = function(err,done){
        var _that = this;
        process.nextTick(function(){
            //取得关系中的模型
            Wxmpautoreply.relations.wxMpAccount.modelTo.find({
                where: {
                    or:[
                        {
                            app_id: _that.app_id
                        },
                        {
                            id: _that.wx_mp_account_id
                        }
                    ]
                }
            },function(db_err,results){
                if(db_err ||results.length <= 0)
                    err();
                else{
                    var singleModel = results[0];
                    _that.app_id = singleModel.app_id;
                    _that.wx_mp_account_id = singleModel.id
                }
                done();
            });
        })
    }

    var _alreadyExists = function(err,done){
        var _that = this;
        process.nextTick(function(){
            Wxmpautoreply.find({
                where:{
                    and:[{
                        key: _that.key
                    },{
                        do: _that.do
                    },{
                        wx_mp_account_id: _that.wx_mp_account_id
                    },{
                        app_id: _that.app_id
                    }]
                }
            },function(db_err,results){
                //在没有通过的情况下，调用 err() 即可
                if(!db_err && results.length > 0){
                    err();
                }
                done();
            })
        })
    }

    //检测do参数是否合法
    Wxmpautoreply.validate('do',function(err){
        if(this.do != enums._auto_reply.do["消息"] && this.do != enums._auto_reply.do["事件"]){
            err();
        }
    },{message: 'is not legal!'});

    //检测type参数是否合法
    Wxmpautoreply.validate('type',function(err){
        var isRight = false;

        for(var i in enums._auto_reply.type){
            if(!isRight){
                isRight = this.type === enums._auto_reply.type[i];
            }
        }

        if(!isRight){
            err();
        }
    },{message: 'is not legal!'});

    //检查账号是否存在
    Wxmpautoreply.validateAsync('wx_mp_account_id',_checkAccountExists,{message: 'is not exists!'})
    Wxmpautoreply.validateAsync('app_id',_checkAccountExists,{message: 'is not exists!'})

    //多条件唯一性检查
    Wxmpautoreply.validateAsync('key',_alreadyExists,{message:'is already exists!'})
};
